//@author A0100088R
//
// **********
//
// delete class deletes tasks from 2 places
//		the vector of tasks
//		the txt storage
//		the currently displayed vector store
//
//
// **********

#include "Delete.h"

Delete::Delete(void) {

}

Delete::~Delete(void) {

}

string Delete::execute(Task & task, vector <Task> & forUIDisplay, vector <Task> & store) {
	ofstream writeFile;
	writeFile.open(FILENAME_LOGGER.c_str(), ofstream::out | ofstream::app);
	writeFile << "Attempting to delete" << endl;
	
	
	// if it is not empty, we are deleting via UNDO function
	if(task.getTaskName() == EMPTY) {
		if(task.getTaskLine() == NIL) {
			throw exception("Invalid line number");
		}
		if(task.getTaskLine() > forUIDisplay.size() || task.getTaskLine() < 0) {
			throw exception("Invalid line number");
		}
	
	
		//collection of data in case user wants to UNDO
		int userTaskLine = task.getTaskLine();
		task.changeTaskName(forUIDisplay[userTaskLine-1].getTaskName());
		task.changeDeadline(forUIDisplay[userTaskLine-1].getDeadline());
		task.changePriority(forUIDisplay[userTaskLine-1].getPriority());
		task.changeTime(forUIDisplay[userTaskLine-1].getTime());
		task.changeStatus(forUIDisplay[userTaskLine-1].getStatus());
		task.changeDateInSeconds(forUIDisplay[userTaskLine-1].getDateInSeconds());
	}

	string taskNameToBeDeleted = task.getTaskName();
	string deadlineToBeDeleted = task.getDeadline();
	string priorityToBeDeleted = task.getPriority();
	string timeToBeDeleted = task.getTime();
	string statusToBeDeleted = task.getStatus();
	time_t secondsToBeDeleted = task.getDateInSeconds();


	
	// entire for loop is to erase from vector<Task> store
	writeFile << "Attempting to delete from store" << endl;
	for(vector<Task>::iterator iter = store.begin(); iter != store.end(); iter++) {
		Task currentTask = *iter;
		
		// all the conditions is to confirm in the case of similar tasknames
		if(currentTask.getTaskName() == taskNameToBeDeleted) {
			if(currentTask.getDeadline() == deadlineToBeDeleted) {
				if(currentTask.getPriority() == priorityToBeDeleted) {
					if(currentTask.getTime() == timeToBeDeleted) {
						if(currentTask.getStatus() == statusToBeDeleted) {
							if(currentTask.getDateInSeconds() == secondsToBeDeleted) {
								store.erase(iter);
								break;
							}
						}
					}
				}
			}
		}
	}

	// erasing from txt file
	vector<string> forTxtRewrite;
	string data;
	Parser parse;
	Storage storage;

	for(vector<Task>::iterator iter = store.begin(); iter != store.end(); iter++) {
		data = parse.convertToString(*iter);
		forTxtRewrite.push_back(data);
	}
	storage.rewrite(forTxtRewrite);


	// erasing from currently displayed list
	writeFile << "Attempting to delete from displayed list" << endl;
	for(vector<Task>::iterator iter = forUIDisplay.begin(); iter != forUIDisplay.end(); iter++) {
		Task currentTask = *iter;
		
		// all the conditions is to confirm in the case of similar tasknames
		if(currentTask.getTaskName() == taskNameToBeDeleted) {
			if(currentTask.getDeadline() == deadlineToBeDeleted) {
				if(currentTask.getPriority() == priorityToBeDeleted) {
					if(currentTask.getTime() == timeToBeDeleted) {
						if(currentTask.getStatus() == statusToBeDeleted) {
							if(currentTask.getDateInSeconds() == secondsToBeDeleted) {
								forUIDisplay.erase(iter);
								break;
							}
						}
					}
				}
			}
		}
	}
	
	writeFile.close();

	// string return is for UI feedback
	data = "deleted '";
	data += taskNameToBeDeleted;
	data += "'";

	return data;
}